package com.springboot.casclient2.config;

import net.unicon.cas.client.configuration.CasClientConfigurationProperties;
import org.jasig.cas.client.authentication.AuthenticationFilter;
import org.jasig.cas.client.session.SingleSignOutFilter;
import org.jasig.cas.client.session.SingleSignOutHttpSessionListener;
import org.jasig.cas.client.util.AbstractCasFilter;
import org.jasig.cas.client.util.HttpServletRequestWrapperFilter;
import org.jasig.cas.client.validation.Cas30ProxyReceivingTicketValidationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.EventListener;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: TongRui乀
 * @Date: 2020-03-25 13:30
 * @description：
 */
@Configuration
public class CasClientConfig {

    @Autowired
    private CasClientConfigurationProperties casClientConfigurationProperties;

    /**
     * 配置登出过滤器(过滤器顺序有要求，先登出-》授权过滤器-》配置过滤验证器-》wraper过滤器)
     * @return
     */
    @Bean
    public FilterRegistrationBean filterSingleRegistration() {
        final FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new SingleSignOutFilter());
        // 设定匹配的路径
        registration.addUrlPatterns("/*");
        Map<String,String>  initParameters = new HashMap<String, String>();
        initParameters.put("casServerUrlPrefix", casClientConfigurationProperties.getServerUrlPrefix());
        registration.setInitParameters(initParameters);
        // 设定加载的顺序
        registration.setOrder(1);
        return registration;
    }

    /**
     *  注册Cas client 的认证过滤器
     * @return
     */
    @Bean
    public FilterRegistrationBean<AbstractCasFilter> filterRegistrationBean(){
        FilterRegistrationBean<AbstractCasFilter> registrationBean = new FilterRegistrationBean();

        registrationBean.setFilter(new AuthenticationFilter());
        // 设置拦截路径
        registrationBean.addUrlPatterns("/*");

        // 设置初始化参数
        Map<String,String> initParameters = new HashMap<String, String>();
        initParameters.put("casServerLoginUrl", casClientConfigurationProperties.getServerLoginUrl());
        initParameters.put("serverName", casClientConfigurationProperties.getClientHostUrl());
        //忽略的url，"|"分隔多个url
        initParameters.put("ignorePattern", "/logout/success|/index");
        registrationBean.setInitParameters(initParameters);
        // 设定加载的顺序
        registrationBean.setOrder(1);
        return registrationBean;
    }

    /**
     * 配置过滤验证器 这里用的是Cas30ProxyReceivingTicketValidationFilter
     * @return
     */
    @Bean
    public FilterRegistrationBean filterValidationRegistration() {
        final FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new Cas30ProxyReceivingTicketValidationFilter());
        // 设定匹配的路径
        registration.addUrlPatterns("/*");
        Map<String,String> initParameters = new HashMap<String, String>();
        initParameters.put("casServerUrlPrefix", casClientConfigurationProperties.getServerUrlPrefix());
        initParameters.put("serverName", casClientConfigurationProperties.getClientHostUrl());
        initParameters.put("useSession", "true");
        registration.setInitParameters(initParameters);
        // 设定加载的顺序
        registration.setOrder(3);
        return registration;
    }

    /**
     * request wraper过滤器
     * @return
     */
    @Bean
    public FilterRegistrationBean filterWrapperRegistration() {
        final FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new HttpServletRequestWrapperFilter());
        // 设定匹配的路径
        registration.addUrlPatterns("/*");
        // 设定加载的顺序
        registration.setOrder(4);
        return registration;
    }

    /**
     * 添加监听器
     * @return
     */
    @Bean
    public ServletListenerRegistrationBean<EventListener> singleSignOutListenerRegistration() {
        ServletListenerRegistrationBean<EventListener> registrationBean = new ServletListenerRegistrationBean<EventListener>();
        registrationBean.setListener(new SingleSignOutHttpSessionListener());
        registrationBean.setOrder(1);
        return registrationBean;
    }


}
